﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace XYTXL.Web.View.zhcx
{
    public partial class zhcx : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindXq();
            }
        }

        private void BindXq()
        {
            string m_whfs = "查询表名";
            SqlParameter[] parameters = new SqlParameter[5];
            parameters[0] = new SqlParameter("@_whfs", m_whfs);
            parameters[1] = new SqlParameter("@_tablecode", "");
            parameters[2] = new SqlParameter("@_fieldslist", "");
            parameters[3] = new SqlParameter("@_query", "");
            parameters[4] = new SqlParameter("@_fs", "");
            DataSet dsxq = XYTXL.API.DbHelperSQL.RunProcedure("pr_zhcx", parameters, "mytable");
            //this.DDtable.DataSource = dsxq;
            this.DDtable.DataSource = dsxq;
            this.DDtable.DataTextField = dsxq.Tables[0].Columns["tablename"].ToString();
            this.DDtable.DataValueField = dsxq.Tables[0].Columns["tablecode"].ToString();
            this.DDtable.DataBind();
        }
        private void BindField()
        {
            string m_whfs, m_tablecode;
            m_whfs = "查询表字段信息";
            m_tablecode = this.DDtable.SelectedValue.ToString();
            SqlParameter[] parameters = new SqlParameter[5];
            parameters[0] = new SqlParameter("@_whfs", m_whfs);
            parameters[1] = new SqlParameter("@_tablecode", m_tablecode);
            parameters[2] = new SqlParameter("@_fieldslist", "");
            parameters[3] = new SqlParameter("@_query", "");
            parameters[4] = new SqlParameter("@_fs", "");
            DataSet dsxq = XYTXL.API.DbHelperSQL.RunProcedure("pr_zhcx", parameters, "mytable");

            this.GVtable.DataSource = dsxq;
            this.GVtable.DataBind();
        }
        //private void BindSyQx()
        //{
        //    string m_yhdm, m_gnmkdm;
        //    m_yhdm = Session["UserID"].ToString();
        //    m_gnmkdm = "web_zhcx_ggcx.aspx";
        //    QxUrl qx = new QxUrl();
        //    int m_qx = qx.SfSyGnmk(m_yhdm, m_gnmkdm);
        //    if (m_qx == 1)
        //        BindQxxx();
        //    else
        //        Response.Write(string.Format(GetRedirect.WINALERT, "抱歉，您无权使用此功能！"));

        //    ViewState["IP"] = Request.ServerVariables["REMOTE_ADDR"];
        //}
        private void BindQxxx()
        {

        }
        //private void BindYhDl()
        //{
        //    string m_yhdm, m_ip, m_gndm;
        //    m_ip = Request.ServerVariables["REMOTE_ADDR"];
        //    m_yhdm = Session["UserID"].ToString();
        //    m_gndm = "web_zhcx_ggcx.aspx";
        //    QxUrl qx = new QxUrl();
        //    int m_qx = qx.YhDl(m_yhdm, m_gndm, m_ip);
        //}


        protected void Btcxzd_Click(object sender, EventArgs e)
        {
            BindField();
        }

        protected void DDtable_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindField();
        }

        protected void Bttj_Click(object sender, EventArgs e)
        {
            int m_cnts, m_xzs, m_next;
            string m_tjlist1, m_tjlist2, m_zdmc, m_tjz, m_tjfh, m_tjgx;
            m_tjlist1 = "(";
            m_tjlist2 = "(";
            m_zdmc = "";
            m_tjz = "";
            m_tjfh = "";
            m_tjgx = "";
            m_cnts = this.GVtable.Rows.Count;
            m_xzs = 0;
            m_next = 1;
            CheckBox chk;
            for (int i = 0; i < m_cnts; i++)
            {
                chk = (CheckBox)this.GVtable.Rows[i].FindControl("CBtjxz");
                if (chk.Checked)
                {
                    if (string.IsNullOrEmpty(((TextBox)this.GVtable.Rows[i].FindControl("Txttj")).Text.ToString()) == false)
                        m_xzs = m_xzs + 1;
                }
            }
            if (m_xzs == 0)
            {
                m_next = 1;
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "alert", "alert('请勾选表中“条件选择”项, 并输入条件值!')", true);
            }
            if (m_next == 1)
            {
                DropDownList DDtj;
                for (int i = 0; i < m_cnts; i++)
                {
                    chk = (CheckBox)this.GVtable.Rows[i].FindControl("CBtjxz");
                    if (chk.Checked)
                    {
                        DDtj = (DropDownList)this.GVtable.Rows[i].FindControl("DDgx");
                        m_tjgx = DDtj.SelectedValue.ToString();

                        m_zdmc = this.GVtable.DataKeys[i].Values["fields"].ToString();
                        m_tjz = ((TextBox)this.GVtable.Rows[i].FindControl("Txttj")).Text.ToString().Trim();
                        DDtj = (DropDownList)this.GVtable.Rows[i].FindControl("DDfh");
                        m_tjfh = DDtj.SelectedValue.ToString();
                        if (m_tjfh == "like")
                            m_tjz = "%" + m_tjz + "%";
                        if (m_tjgx == "and")
                            m_tjlist1 = m_tjlist1 + " " + m_zdmc + " " + m_tjfh + " '" + m_tjz + "' and ";
                        else
                            m_tjlist2 = m_tjlist2 + " " + m_zdmc + " " + m_tjfh + " '" + m_tjz + "' or ";
                    }
                }
                m_tjlist1 = m_tjlist1.Trim();
                m_tjlist2 = m_tjlist2.Trim();
                int m_len;
                StringList strlist = new StringList();
                string strtj = strlist.GetLastStr(m_tjlist1, 3);
                if (strtj == "and")
                {
                    m_len = m_tjlist1.Length;
                    m_tjlist1 = m_tjlist1.Substring(0, m_len - 3);
                }

                strtj = strlist.GetLastStr(m_tjlist2, 2);
                if (strtj == "or")
                {
                    m_len = m_tjlist2.Length;
                    m_tjlist2 = m_tjlist2.Substring(0, m_len - 2);
                }
                m_tjlist1 = m_tjlist1 + " )";
                m_tjlist2 = m_tjlist2 + " )";
                if (m_tjlist1 != "( )" && m_tjlist2 == "( )")
                    this.Txttj.Text = m_tjlist1;
                if (m_tjlist1 == "( )" && m_tjlist2 != "( )")
                    this.Txttj.Text = m_tjlist2;
                if (m_tjlist1 != "( )" && m_tjlist2 != "( )")
                    this.Txttj.Text = m_tjlist1 + " or " + m_tjlist2;


            }
        }

        protected void BTxz_Click(object sender, EventArgs e)
        {
            int m_cnts;
            m_cnts = this.GVtable.Rows.Count;
            CheckBox chk;
            Button btn = sender as Button;
            for (int i = 0; i < m_cnts; i++)
            {
                chk = (CheckBox)this.GVtable.Rows[i].FindControl("CBxz");
                if (btn.Text == "全选")
                    chk.Checked = true;
                else
                    chk.Checked = false;
            }
            if (btn.Text == "全选")
                btn.Text = "取消";
            else
                btn.Text = "全选";

            BindFields();
        }
        private void BindFields()
        {
            int m_cnts;
            string m_fieldlist = "";
            m_cnts = this.GVtable.Rows.Count;
            CheckBox chk;
            for (int i = 0; i < m_cnts; i++)
            {
                chk = (CheckBox)this.GVtable.Rows[i].FindControl("CBxz");
                if (chk.Checked)
                {
                    m_fieldlist = m_fieldlist + this.GVtable.DataKeys[i].Values["fields"].ToString() + ",";
                }
            }
            m_fieldlist = m_fieldlist.Trim();
            int m_len = m_fieldlist.Length;
            StringList fields = new StringList();
            string strfld = fields.GetLastStr(m_fieldlist, 1);
            if (strfld == ",")
                this.Txtfields.Text = m_fieldlist.Substring(0, m_len - 1);
            else
                this.Txtfields.Text = m_fieldlist;
        }

        protected void Btcxxx_Click(object sender, EventArgs e)
        {
            string m_whfs, m_tablecode, m_fields, m_query, m_fs;
            m_whfs = "按条件查询";
            m_tablecode = this.DDtable.SelectedValue.ToString().Trim();
            m_fields = this.Txtfields.Text.ToString().Trim();
            m_query = this.Txttj.Text.ToString().Trim();
            m_fs = "1";
            if (this.CBzdxz.Checked == false)
                m_fs = "0";
            if (string.IsNullOrEmpty(m_fields) || string.IsNullOrEmpty(m_query))
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "alert", "alert('请生成查询条件和选择输入字段!')", true);
            }
            else
            {
                //this.Txttj.Text = m_tablecode + "_______" + m_query + "________" + m_fields.ToString();
                SqlParameter[] parameters = new SqlParameter[5];
                parameters[0] = new SqlParameter("@_whfs", m_whfs);
                parameters[1] = new SqlParameter("@_tablecode", m_tablecode);
                parameters[2] = new SqlParameter("@_fieldslist", m_fields);
                parameters[3] = new SqlParameter("@_query", m_query);
                parameters[4] = new SqlParameter("@_fs", m_fs);
                DataSet dsxq = XYTXL.API.DbHelperSQL.RunProcedure("pr_zhcx", parameters, "mytable");
                this.GVdata.DataSource = dsxq;
                this.GVdata.DataBind();
                dsxq.Dispose();
            }
        }

        protected void CBxz_CheckedChanged(object sender, EventArgs e)
        {
            BindFields();
        }

        protected void dataout_Click(object sender, EventArgs e)
        {
            Response.Buffer = true;
            Response.Charset = "GB2312";
            string OutPutName = DateTime.Now.ToString("yyyyMMdd");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + OutPutName + ".xls");

            Response.ContentEncoding = System.Text.Encoding.UTF8;
            //设置输出文件类型为excel文件。 
            Response.ContentType = "application/ms-excel";
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.GVdata.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
        protected void GVkbkmd_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                    e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
            }
        }


        protected void CBtjxz_CheckedChanged(object sender, EventArgs e)
        {
            BindShkcColor();
        }
        private void BindShkcColor()
        {
            int m_cnts;
            CheckBox chk;
            m_cnts = this.GVtable.Rows.Count;
            for (int i = 0; i < m_cnts; i++)
            {
                chk = (CheckBox)this.GVtable.Rows[i].FindControl("CBtjxz");
                if (chk.Checked)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        this.GVtable.Rows[i].Cells[j].BackColor = System.Drawing.Color.Silver;
                    }
                }
                else
                {
                    for (int j = 0; j < 6; j++)
                    {
                        this.GVtable.Rows[i].Cells[j].BackColor = System.Drawing.Color.White;
                    }
                }
            }
        }
    }
}